package com.tony.mulitithread.controller;

import com.google.common.collect.Lists;
import com.tony.mulitithread.domain.FileInfo;
import com.tony.mulitithread.domain.FileInfo2;
import com.tony.mulitithread.domain.FileInfo3;
import com.tony.mulitithread.service.FileService;
import com.tony.mulitithread.utils.AsyncTaskUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/fileInsert")
public class FileInsertController {
    @Resource
    private FileService fileService;

    @GetMapping("/insertBatch")
    public String insertBatch(){
        List<FileInfo> list = new ArrayList<>();
        for (int i = 1; i <= 100000; i++) {
            FileInfo fileInfo = new FileInfo();
            fileInfo.setFileId(""+i);
            fileInfo.setFileName("测试" + i);
            fileInfo.setFileUrl("测试" + i);
            fileInfo.setFileSuffix("测试" + i);
            fileInfo.setFileSize(i);
            fileInfo.setRelationType(1);
            fileInfo.setRelationId("cheshi");
            fileInfo.setCreateTime(new Date());
            fileInfo.setStatus(0);
            fileInfo.setSourceStatus(1);
            list.add(fileInfo);
        }
        int i = fileService.insertBatch(list);
        return "插入"+i+"行";
    }

    @GetMapping("/insertBatchMultiThread")
    public String insertBatchMultiThread(){
        List<FileInfo2> list = new ArrayList<>();
        for (int i = 1; i <= 100000; i++) {
            FileInfo2 fileInfo2 = new FileInfo2();
            fileInfo2.setFileId(""+i);
            fileInfo2.setFileName("测试" + i);
            fileInfo2.setFileUrl("测试" + i);
            fileInfo2.setFileSuffix("测试" + i);
            fileInfo2.setFileSize(i);
            fileInfo2.setRelationType(1);
            fileInfo2.setRelationId("cheshi");
            fileInfo2.setCreateTime(new Date());
            fileInfo2.setStatus(0);
            fileInfo2.setSourceStatus(1);
            list.add(fileInfo2);
        }

        // 分为 n 份，每份 2000条
        List<List<FileInfo2>> listPartition = Lists.partition(list, 2000);

        Object[] temp = new Object[listPartition.size()];

        // 分片
        for (int i=0;i<listPartition.size();i++) {
            List<FileInfo2> item=listPartition.get(i);
            temp[i] = item;
            //这里面放所有。要放在一个Object[]中才行。但是注意，放在这个args中的对象的实际类要和userService中对应的那个方法对象才行。注意，是真实类型哈。比如上面这个item，他是Lists.partition(lists, 2000)创建出来的List中的一个元素，其真实类型是java.util.ArrayList$SubList,所以，userService中的updateBatch4方法的形参类型就得是ArrayList.SubList，不能是List，不然会报错。但是因为，那个ArrayList.SubList是ArrayList的私有属性，所以updateBatch4方法的形参类型写不了ArrayList.SubList，所以，这里我才用了上面这个做法，即，传数组。
            Object[] args = new Object[]{temp, i};
            //具体参数为：类，方法，方法参数
            AsyncTaskUtil.asyncTask(fileService, "insertBatch2", args);
        }

//        return "插入"+i+"行";
        return "";
    }

    @GetMapping("/insertBatchMultiThread2")
    public String insertBatchMultiThread2(){
        List<FileInfo3> list = new ArrayList<>();
        for (int i = 1; i <= 100000; i++) {
            FileInfo3 fileInfo3 = new FileInfo3();
            fileInfo3.setFileId(""+i);
            fileInfo3.setFileName("测试" + i);
            fileInfo3.setFileUrl("测试" + i);
            fileInfo3.setFileSuffix("测试" + i);
            fileInfo3.setFileSize(i);
            fileInfo3.setRelationType(1);
            fileInfo3.setRelationId("cheshi");
            fileInfo3.setCreateTime(new Date());
            fileInfo3.setStatus(0);
            fileInfo3.setSourceStatus(1);
            list.add(fileInfo3);
        }

        // 分为 n 份，每份 2000条
        List<List<FileInfo3>> listPartition = Lists.partition(list, 2000);

        // 分片
        for (List<FileInfo3> item : listPartition) {
            AsyncTaskUtil.asyncTask(new InsertFile(item,fileService));
        }
        return "";
    }
}
